import { defineStore } from 'pinia'
import http from '../utils/http'

export const useConfigStore = defineStore('config', {
    state: () => ({
        initialized: false,
        port: '配置正在加载...',
        baudrate: 0,
        timeout: 0,
        ask_times: 0,
        ask_interval: 0,
        wait_time: 0,
        max_device_num: 0,
        // port: '/dev/ttyUSB0',
        // baudrate: 9600,
        // timeout: 0.03,
        // ask_times: 10,
        // ask_interval: 0.03,
        // wait_time: 0,
        // max_device_num: 99,
    }),
    actions: {
        async initConfig() {
            const res = await http.get('/config')
            if (res.success) {
                this.$patch(res.config)
                this.initialized = true
                return true
            }
            return false
        },
        async setValue(key, value) {
            const res = await http.get('/config', {
                params: {
                    key,
                    value,
                },
            })
            if (res.success) {
                this[key] = value
                return true
            }

            return false
        },
    },
})
